package com.uam.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.uam.db.model.entity.Point;

import java.util.ArrayList;
import java.util.List;

/**
 * @author uam
 * @create 2023/12/3 14:18
 * @email uam@uam.com
 */
public class JsonUtils {
    public static List<Point> getPointlist(String json) {
        ArrayList<Point> points = new ArrayList<>();
        JSONObject jsonObject = JSON.parseObject(json);
        JSONArray features = jsonObject.getJSONArray("features");
        features.forEach(feature -> {
            Point point = new Point();
            List<Float> positionlist = new ArrayList<>();
            String string = feature.toString();
            JSONObject jsonObject1 = JSON.parseObject(feature.toString());
            JSONObject geometry = jsonObject1.getJSONObject("geometry");
            JSONObject properties = jsonObject1.getJSONObject("properties");
            JSONArray coordinates = geometry.getJSONArray("coordinates");
            coordinates.forEach(coordinate -> {
                positionlist.add(Float.valueOf(coordinate.toString()));
            });
            String name = properties.getString("Name");
            Integer floor = properties.getInteger("Floor");
            Float height = properties.getFloat("Height");
            Integer status = properties.getInteger("Status");
            positionlist.add(height);
            String position = StringUtils.join(positionlist, ",");
            point.setPosition(position);
            point.setPointName(name);
            point.setStatus(status);
            point.setFloor(floor);
            point.setHeight(height);
            points.add(point);
        });
        return points;
    }


    public static JSONObject pointToGeoJson(List<Point> list, String name) {
        JSONObject jsonObject = getGeoJsonDefault(name);
        if (list == null) {
            return jsonObject;
        }
        JSONArray features = jsonObject.getJSONArray("features");
        list.forEach(point -> {
            JSONObject root = new JSONObject(16);
            JSONArray lonLat = new JSONArray();
            String[] arr = point.getPosition().split("\\,");
            int arrLength = arr.length;
            for (int i = 0; i < arrLength; i++) {
                lonLat.add(Double.valueOf(arr[i]));
            }
            JSONObject geometry = new JSONObject();
            geometry.put("type", "Point");
            geometry.put("coordinates", lonLat);
            root.put("geometry", geometry);
            root.put("type", "Feature");
            JSONObject properties = new JSONObject(6);
            properties.put("Status", point.getStatus());
            properties.put("Floor", point.getFloor());
            properties.put("Height", point.getHeight());
            properties.put("id", point.getPointId());
            properties.put("Name", point.getPointName());
            properties.put("lnglat",lonLat);
            root.put("properties", properties);
            features.add(root);
        });
        return jsonObject;
    }

    public static JSONObject getGeoJsonDefault(String name) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("type", "GeometryCollection");
        jsonObject.put("name", name);
        jsonObject.put("features", new JSONArray(1024));
        return jsonObject;
    }
   /* public static void main(String[] args) {
        String s = "{" +
                "\"type\" : \"FeatureCollection\"," +
                "\"name\" : \"PointOnAreaOverlayer_Point\"," +
                "\"features\" : [" +
                "{" +
                "\"type\" : \"Feature\"," +
                "\"geometry\" : {" +
                "\"type\" : \"Point\"," +
                "\"coordinates\" : [ 113.0609862212, 28.0563236078 ]" +
                "}," +
                "\"properties\" : {" +
                "\"Name\" : \"雇家冲\"," +
                "\"Floor\" : 0," +
                "\"Height\" : 0," +
                "\"Status\" : 1" +
                "}" +
                "}," +
                "{" +
                "\"type\" : \"Feature\"," +
                "\"geometry\" : {" +
                "\"type\" : \"Point\"," +
                "\"coordinates\" : [ 113.0445224505, 28.070473891 ]" +
                "}," +
                "\"properties\" : {" +
                "\"Name\" : \"长沙商贸旅游职业技术学院\"," +
                "\"Floor\" : 0," +
                "\"Height\" : 0," +
                "\"Status\" : 1" +
                "}" +
                "}," +
                "{" +
                "\"type\" : \"Feature\"," +
                "\"geometry\" : {" +
                "\"type\" : \"Point\"," +
                "\"coordinates\" : [ 113.1069282974, 27.9979401623 ]" +
                "}," +
                "\"properties\" : {" +
                "\"Name\" : \"跳马小学\"," +
                "\"Floor\" : 0," +
                "\"Height\" : 0," +
                "\"Status\" : 1" +
                "}" +
                "}" +
                "" +
                "]" +
                "}";
        List<Point> pointlist = new JsonUtils().getPointlist(s);
        pointlist.forEach(System.out::println);


    }*/
}
